<?php
/**
 * Goods
 * Created by Veyron
 * motto:现在的拼搏 为了更好的明天
 * day:  2020/2/8
 * Time: 21:02
 */

namespace app\admin\controller;
use app\common\business\Goods as GoodsBis;

class Goods extends AdminBase
{
    public function index(){

        $data = [];
        $title = input('param.title','',"trim");
        $time  = input('param.time','',"trim");
        $param = '';
        if(!empty($title)){
            $data['title'] = $title;
            $param .= '&title='.$title;
        }
        if(!empty($time)){
            $data['create_time'] = explode('-',$time);
            $param .= '&time='.$time;
        }

        $goods = (new GoodsBis())->getLists($data,config('admin.adminGoodsPageSize'));

        return view('',[
            'goods'  => $goods,
            'param'  => $param
        ]);
    }
    public function add(){
        return view();
    }

    /**
     * 商品基本信息以及sku信息入库
     * @return \think\response\Json
     */
    public function save(){
        if(!$this->request->isPost()){
            return show(config('status.error'),'请求不合法');
        }
        $data = input('param.');

        //token验证（防止‘跨站伪造攻击（crsf）’）
        $tokenCheck = $this->request->checkToken('__token__');

        if(!$tokenCheck){
            return show(config('status.error'),'非法请求');
        }
        //数据验证
        $vaildate = new \app\admin\validate\Goods();
        if(!$vaildate->check($data)){
            return show(config('status.error'),$vaildate->getError());
        }

        //数据处理
        // 获取分类最后一个值
        $category_path_id = $data['category_id'];
        $result = explode(',', $category_path_id);
        $category_id = array_search('undefined',$result);
        //前端bug——选两个分类第三个是undefined 简单处理下,有可能有两个undefined所以过两遍
        if($category_id){
            unset($result[$category_id]);
        }
        $category_id = array_search('undefined',$result);
        if($category_id){
            unset($result[$category_id]);
        }

        $data['category_path_id'] = implode(',',$result);
        $data['category_id'] = end($result);


        $res = (new GoodsBis())->insertData($data);

        if(!$res){
            return show(config('status.error'),'商品上传失败');
        }
        return show(config('status.success'),'商品上传成功');
    }

//    /**
//     * 修改装状态 （改到了 AdminBase->status中）
//     * @return \think\response\Json
//     */
//    public function status(){
//        $obj = new GoodsBis();
//        return $this->ChangeStatus($obj);
//    }

//    /**
//     * 修改装状态 （改到了 AdminBase->status中）
//     * @return \think\response\Json
//     */
//    public function isIndexRecommend(){
//        $obj = new GoodsBis();
//        return $this->ChangeStatus($obj,'is_index_recommend');
//    }
}
